#include "widget.h"
#include "ui_widget.h"
#include <QDebug>
#include <QRegExpValidator>

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);

    ui->lineEdit_name->setPlaceholderText("请输入姓名");
    ui->lineEdit_name->setClearButtonEnabled(true);

    ui->lineEdit_psword->setPlaceholderText("请输入密码");
    ui->lineEdit_psword->setClearButtonEnabled(true);
    ui->lineEdit_psword->setEchoMode(QLineEdit::Password);

    ui->lineEdit_phone->setPlaceholderText("请输入电话");
    ui->lineEdit_phone->setClearButtonEnabled(true);
//    ui->lineEdit_phone->setInputMask("00000000000");

    QRegExp regExp("^1\\d{10}$");
    ui->lineEdit_phone->setValidator(new QRegExpValidator(regExp));

    ui->lineEdit1->setEchoMode(QLineEdit::Password);
    ui->lineEdit2->setEchoMode(QLineEdit::Password);
}

Widget::~Widget()
{
    delete ui;
}

void Widget::compare()
{
    QString text1 = ui->lineEdit1->text();
    QString text2 = ui->lineEdit2->text();
    if (text1.isEmpty() || text2.isEmpty())
        ui->label_password->setText("请输入密码");
    else if (text1 != text2)
        ui->label_password->setText("输入的密码不一致");
    else
        ui->label_password->setText("输入的密码一致");
}


void Widget::on_pushButton_clicked()
{
    QString gender = ui->radioButton_male->isChecked() ? "male" : "female";
    qDebug() << "姓名: " << ui->lineEdit_name->text()
             << "密码: " << ui->lineEdit_psword->text()
             << "性别: " << gender
             << "手机: " << ui->lineEdit_phone->text();
//    ui->lineEdit_name->setC
}

void Widget::on_lineEdit_phone_textEdited(const QString &arg1)
{
    QString text(arg1);
    int pos = 0;
    if (ui->lineEdit_phone->validator()->validate(text, pos) == QValidator::Acceptable)
        ui->pushButton->setEnabled(true);
    else
        ui->pushButton->setEnabled(false);
}

void Widget::on_lineEdit1_textEdited(const QString &arg1)
{
    (void) arg1;
    compare();
}

void Widget::on_lineEdit2_textEdited(const QString &arg1)
{
    (void) arg1;
    compare();
}
